/**
 * Created by python on 19-2-27.
 */
const HOST = 'http://127.0.0.1:8000';

function get_query_string(key, defval) {
    let reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)', 'i');
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return decodeURI(r[2]);
    }
    return defval;
};



//问答回复的时间
Vue.filter("date", function (value,server_time) {
    let oldDate = new Date(value);
    let newDate = new Date(server_time);
    // console.log(newDate.getTime())
    // console.log((newDate.getTime()) / 1000)
    var dayNum = '';
    var getTime = (newDate.getTime() - oldDate.getTime()) / 1000;
    if (getTime < 60 * 5) {
        dayNum = "刚刚";
    } else if (getTime >= 60 * 5 && getTime < 60 * 60) {
        dayNum = parseInt(getTime / 60) + "分钟前";
    } else if (getTime >= 3600 && getTime < 3600 * 24) {
        dayNum = parseInt(getTime / 3600) + "小时前";
    } else if (getTime >= 3600 * 24 && getTime < 3600 * 24 * 30) {
        dayNum = parseInt(getTime / 3600 / 24) + "天前";
    } else if (getTime >= 3600 * 24 * 30 && getTime < 3600 * 24 * 30 * 12) {
        dayNum = parseInt(getTime / 3600 / 24 / 30) + "个月前";
    } else if (getTime >= 3600 * 24 * 30 * 12) {
        dayNum = parseInt(getTime / 3600 / 24 / 30 / 12) + "年前";
    }
    // let year = oldDate.getFullYear();
    // let month = oldDate.getMonth();
    // let day = oldDate.getDate();
    // let hour = oldDate.getHours();
    // let minute = oldDate.getMinutes();
    // let second = oldDate.getSeconds();
    return dayNum
});



//问答创建时间转换过滤器
    Vue.filter('date2ymdhm', function (value) {
        return value.getFullYear() + "-" + (value.getMonth() + 1) + "-" + value.getDate() + " " + value.getHours() + ":" + value.getMinutes();
    });


//将时间戳转换为字符串格式的过滤器
    Vue.filter('str2date', function (value) {
    return new Date(value);
});

// // 获取cookie
// Vue.prototype.getCookie = function (name) {
//     var arr;
//     var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
//     if (arr = document.cookie.match(reg))
//         return unescape(arr[2]);
//     else
//         return null;
//     };
// // axios.defaults.withCredentials = true;
//
// //删除cookie
// function delCookie(name) {
//     var exp = new Date();
//     exp.setTime(exp.getTime() - 1);
//     var cval = getCookie(name);
//     if (cval != null)
//         document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
// };

function setCookie(name, value, days) {

    var d = new Date;

    d.setTime(d.getTime() + 24*60*60*1000*days);

    window.document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();

}

function getCookie(name) {

    var v = window.document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');

    return v ? v[2] : null;

}

function delCookie(name) {

    this.set(name, '', -1);

}

// 字符串转日期类型   str2date
// 日期类型得到年元日  date2ymd
// 日期类型得到时分  date2hm

Vue.filter('date2ymd', function (value) {
    return value.getFullYear() + "-" + (value.getMonth() + 1) + "-" + value.getDate();
});
Vue.filter('date2hm', function (value) {
    return value.getHours() + ":" + value.getMinutes() ;
});